package com.wind.taobao.api.trade.taobaoke.convert
{
	import com.wind.taobao.api.trade.shop.model.TaobaoShop;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * TaobaoShop 转换器
	 * @author fxy
	 *
	 */
	public class TaobaokeShopConvert
	{
		/**
		 * 转换Shop集合
		 */
		public static function convertTaobaokeShops(value:Object, type:String="json"):ArrayCollection
		{
			var shops:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							shops.addItem(convertTaobaokeShop((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return shops;
		}

		/**
		 * 转换单个Shop对象
		 * value:XML 或 JSON
		 */
		public static function convertTaobaokeShop(value:Object, type:String="json", shop:TaobaoShop=null):TaobaoShop
		{
			if (shop == null)
				shop=new TaobaoShop();
			if (value == null)
				return shop;
			if (type == TaobaoConstants.XML)
			{
				shop=convertXMLToTaobaokeShop(XML(value), shop);
			}
			else
			{
				shop=convertJSONToTaobaokeShop(value, shop);
			}
			return shop;
		}

		/**
		 * JSON转换为Shop
		 */
		private static function convertJSONToTaobaokeShop(json:Object, shop:TaobaoShop):TaobaoShop
		{
			if (json.hasOwnProperty(TaobaoConstants.USER_ID))
			{
				shop.user_id=json[TaobaoConstants.USER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.CLICK_URL))
			{
				shop.click_url=json[TaobaoConstants.CLICK_URL];
			}
			if (json.hasOwnProperty(TaobaoConstants.SHOP_COMMISSION_RATE))
			{
				shop.shop_commission_rate=json[TaobaoConstants.SHOP_COMMISSION_RATE];
			}
			return shop;
		}



		/**
		 * XML转换为Shop
		 */
		private static function convertXMLToTaobaokeShop(value:XML, shop:TaobaoShop):TaobaoShop
		{
			return shop;
		}

	}
}